

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>ccqw.interp &mdash; CCQW 0.1.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/sidebar.js"></script>
    <link rel="top" title="CCQW 0.1.1 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for ccqw.interp</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot; Our interpolation functions</span>

<span class="sd">    :Authors:</span>
<span class="sd">      - 20081016 Nicola Creati</span>
<span class="sd">      - 20081016 Roberto Vidmar</span>

<span class="sd">    :Revision:  $Revision: 52 $</span>

<span class="sd">    :Copyright: 2011-2012</span>
<span class="sd">                Nicola Creati &lt;ncreati@inogs.it&gt;</span>
<span class="sd">                Roberto Vidmar &lt;rvidmar@inogs.it&gt;</span>

<span class="sd">    :License: MIT/X11 License (see :download:`license.txt</span>
<span class="sd">                               &lt;../../license.txt&gt;`)</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<div class="viewcode-block" id="interp2d"><a class="viewcode-back" href="../../ccqw.html#ccqw.interp.interp2d">[docs]</a><span class="k">def</span> <span class="nf">interp2d</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">ext</span><span class="p">,</span> <span class="n">xy</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return a new 2d matrix of linear inetrpolated values at xy[i, j].</span>

<span class="sd">      z is a 2d array of values at evenly spaced values in</span>
<span class="sd">      ext = ((xmin, xmax), (ymin, ymax))</span>

<span class="sd">      :param z: 2d array of evenly spaced values</span>
<span class="sd">      :type z: numpy array</span>
<span class="sd">      :param ext: ((xmin, xmax), (ymin, ymax))</span>
<span class="sd">      :type ext: tuple</span>
<span class="sd">      :param xy: 2d matrix</span>
<span class="sd">      :type xy: numpy array</span>
<span class="sd">      :returns: new 2d matrix of linear inetrpolated values at xy[i, j]</span>
<span class="sd">      :rtype: numpy array</span>
<span class="sd">      :raises:</span>

<span class="sd">      .. warning:: All xy points must fit into ext rectangle.</span>
<span class="sd">                   **NO CHECK IS MADE!**</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="p">(</span><span class="n">xmin</span><span class="p">,</span> <span class="n">xmax</span><span class="p">),</span> <span class="p">(</span><span class="n">ymin</span><span class="p">,</span> <span class="n">ymax</span><span class="p">)</span> <span class="o">=</span> <span class="n">ext</span>
  <span class="k">if</span> <span class="n">xmin</span> <span class="o">&gt;</span> <span class="n">xmax</span> <span class="ow">or</span> <span class="n">ymin</span> <span class="o">&gt;</span> <span class="n">ymax</span><span class="p">:</span>
    <span class="k">raise</span> <span class="ne">ValueError</span> <span class="p">(</span><span class="s">&quot;Wrong limits: </span><span class="si">%s</span><span class="s"> &gt; </span><span class="si">%s</span><span class="s"> or </span><span class="si">%s</span><span class="s"> &gt; </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
      <span class="n">xmin</span><span class="p">,</span> <span class="n">xmax</span><span class="p">,</span> <span class="n">ymin</span><span class="p">,</span> <span class="n">ymax</span><span class="p">))</span>
  <span class="n">nrows</span><span class="p">,</span> <span class="n">ncols</span> <span class="o">=</span> <span class="n">z</span><span class="o">.</span><span class="n">shape</span>

  <span class="c"># Compute grid spacing in both directions</span>
  <span class="n">dx</span> <span class="o">=</span> <span class="p">(</span><span class="n">xmax</span> <span class="o">-</span> <span class="n">xmin</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">ncols</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span>
  <span class="n">dy</span> <span class="o">=</span> <span class="p">(</span><span class="n">ymax</span> <span class="o">-</span> <span class="n">ymin</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">nrows</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span>

  <span class="n">c</span><span class="p">,</span> <span class="n">t</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">((</span><span class="n">xy</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">xmin</span><span class="p">),</span> <span class="n">dx</span><span class="p">)</span>
  <span class="n">r</span><span class="p">,</span> <span class="n">u</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">((</span><span class="n">xy</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">ymin</span><span class="p">),</span> <span class="n">dy</span><span class="p">)</span>

  <span class="n">t</span> <span class="o">=</span> <span class="n">t</span> <span class="o">/</span> <span class="mf">10.</span>
  <span class="n">u</span> <span class="o">=</span> <span class="n">u</span> <span class="o">/</span> <span class="mf">10.</span>

  <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
  <span class="n">c</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>

  <span class="n">newz</span>      <span class="o">=</span> <span class="p">((</span><span class="mi">1</span> <span class="o">-</span> <span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">z</span><span class="p">[</span><span class="n">r</span><span class="p">,</span>     <span class="n">c</span>    <span class="p">]</span> <span class="o">+</span>
                  <span class="n">t</span>    <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">z</span><span class="p">[</span><span class="n">r</span><span class="p">,</span>     <span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">+</span>
                  <span class="n">t</span>    <span class="o">*</span>    <span class="n">u</span>    <span class="o">*</span> <span class="n">z</span><span class="p">[</span><span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">c</span>    <span class="p">]</span> <span class="o">+</span>
               <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">t</span><span class="p">)</span> <span class="o">*</span>    <span class="n">u</span>    <span class="o">*</span> <span class="n">z</span><span class="p">[</span><span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
  <span class="k">return</span>  <span class="n">newz</span>
</div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

  <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">],</span>
                <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">],</span>
                <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">],</span>
                <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">],</span>
                <span class="p">[</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">]])</span>

  <span class="n">zext</span> <span class="o">=</span> <span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">))</span>
  <span class="k">print</span> <span class="n">zext</span>
  <span class="n">xy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">15.0</span><span class="p">,</span> <span class="mf">35.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">12.</span><span class="p">,</span> <span class="mf">31.</span><span class="p">],</span> <span class="p">[</span><span class="mf">18.5</span><span class="p">,</span> <span class="mf">39.5</span><span class="p">],</span> <span class="p">[</span><span class="mf">16.45</span><span class="p">,</span> <span class="mf">36.1</span><span class="p">]])</span>
  <span class="k">print</span> <span class="s">&quot;xy=&quot;</span><span class="p">,</span> <span class="n">xy</span>
  <span class="k">print</span> <span class="s">&quot;z=&quot;</span><span class="p">,</span> <span class="n">z</span>

  <span class="n">newz</span> <span class="o">=</span> <span class="n">interp2d</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">zext</span><span class="p">,</span> <span class="n">xy</span><span class="p">)</span>
  <span class="k">print</span> <span class="s">&quot;newz=&quot;</span><span class="p">,</span> <span class="n">newz</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/ogs.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Roberto Vidmar, Nicola Creati.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>